﻿using ServiceWeb.DBGateway;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using Microsoft.EntityFrameworkCore;
using System.Reflection.Emit;
using UtilsORM;
using UtilsORM.MySqlData;
using ServiceWeb.DBBase;

namespace ServiceWeb.WebAdmin
{
    public class ServiceProduct
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        public void createTaskPlainByTemplate(string tpguid, string cguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();

            //try
            //{
            iot_device_task_plain template_plain = ef.iot_device_task_plain.Where(c => c.tp_guid == tpguid && c.tp_is_template == 1).FirstOrDefault();


            iot_product scene = ef.iot_product.Where(c => c.p_guid == template_plain.tp_p_guid).FirstOrDefault();
            scene.p_id = 0;
            scene.p_guid = Guid.NewGuid().ToString();
            scene.p_name = scene.p_name + "-" + DateTime.Now.ToString("yyyyMMddHHmmss");

            //查编码
            iot_product getcode = ef.iot_product.OrderByDescending(c => c.p_id).FirstOrDefault();
            if (getcode != null)
            {
                scene.p_code = (int.Parse(getcode.p_code) + 1).ToString();
            }
            else
            {
                scene.p_code = "1001";
            }
            scene.p_check_code = scene.p_code + new UtilsStringQuery().GetRandNumber2();
            scene.p_add_time = DateTime.Now;
            scene.p_c_guid = cguid;
            scene.p_remark = "";
            ef.iot_product.Add(scene);
            ef.SaveChanges();

            //写入device_task_plain
            iot_device_task_plain addplain = new iot_device_task_plain();
            addplain.tp_guid = Guid.NewGuid().ToString();
            addplain.tp_title = template_plain.tp_title + "-" + DateTime.Now.ToString("yyyyMMddHHmmss");
            addplain.tp_status = template_plain.tp_status;
            addplain.tp_mod_time = DateTime.Now;
            addplain.tp_add_time = DateTime.Now;
            addplain.tp_c_guid = cguid;
            addplain.tp_is_template = 1;
            addplain.tp_p_guid = scene.p_guid;
            ef.iot_device_task_plain.Add(addplain);
            ef.SaveChanges();


            //生成设备
            List<iot_device> list = ef.iot_device.Where(c => c.d_p_guid == template_plain.tp_p_guid && c.d_is_template == 1).ToList();

            //生成网关
            foreach (iot_device device in list)
            {
                iot_device_model model = ef.iot_device_model.Where(c => c.m_guid == device.d_model_guid).FirstOrDefault();
                if (model.m_attribute == 3)
                {
                    iot_device adddevice = new UtilsClassClone().Clone(device);
                    //生成编码
                    adddevice.d_id = 0;
                    adddevice.d_name = device.d_name + "-" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    adddevice.d_guid = Guid.NewGuid().ToString();
                    adddevice.d_p_guid = scene.p_guid;
                    adddevice.d_is_template = 1;
                    adddevice.d_add_time = DateTime.Now;
                    adddevice.d_mod_time = DateTime.Now;
                    adddevice.d_gw_d_guid = "";
                    MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                    magerLoginInfo.userCompanyGuid = cguid;
                    new DBIotDevice().saveAll(magerLoginInfo, adddevice);


                    iot_device_gateway lastRecord = ef.iot_device_gateway.OrderByDescending(c => c.gw_id).FirstOrDefault();

                    iot_device_gateway gateway = ef.iot_device_gateway.Where(c => c.gw_d_guid == device.d_guid).FirstOrDefault();
                    if (gateway != null)
                    {
                        gateway.gw_id = 0;
                        gateway.gw_guid = Guid.NewGuid().ToString();
                        gateway.gw_d_guid = adddevice.d_guid;
                        gateway.gw_add_time = DateTime.Now;
                        gateway.gw_mod_time = DateTime.Now;
                        gateway.gw_deviceid = "";
                        if (lastRecord != null)
                        {
                            gateway.gw_code = (int.Parse(lastRecord.gw_code) + 1).ToString();
                        }
                        else
                        {
                            gateway.gw_code = "1010";
                        }
                        ef.iot_device_gateway.Add(gateway);
                        ef.SaveChanges();



                        //写入网关下所有设备
                        List<iot_device> gateWayDevicelist = list.Where(c => c.d_gw_d_guid == device.d_guid).ToList();
                        foreach (iot_device item in gateWayDevicelist)
                        {
                            iot_device gateWayDevice = new UtilsClassClone().Clone(item);
                            //生成编码
                            gateWayDevice.d_id = 0;
                            gateWayDevice.d_name = gateWayDevice.d_name + "-" + DateTime.Now.ToString("yyyyMMddHHmmss");
                            gateWayDevice.d_guid = Guid.NewGuid().ToString();
                            gateWayDevice.d_p_guid = scene.p_guid;
                            gateWayDevice.d_is_template = 1;
                            gateWayDevice.d_add_time = DateTime.Now;
                            gateWayDevice.d_mod_time = DateTime.Now;
                            gateWayDevice.d_gw_d_guid = gateway.gw_d_guid;
                            magerLoginInfo = new MagerLoginInfo();
                            magerLoginInfo.userCompanyGuid = cguid;
                            new DBIotDevice().saveAll(magerLoginInfo, gateWayDevice);
                        }
                    }
                }
                else if (string.IsNullOrWhiteSpace(device.d_gw_d_guid))
                {
                    //不是网关，又没有绑定网关的设备
                    device.d_id = 0;
                    device.d_name = device.d_name + "-" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    device.d_guid = Guid.NewGuid().ToString();
                    device.d_p_guid = scene.p_guid;
                    device.d_is_template = 1;
                    device.d_add_time = DateTime.Now;
                    device.d_mod_time = DateTime.Now;
                    device.d_gw_d_guid = "";
                    MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                    magerLoginInfo.userCompanyGuid = cguid;
                    new DBIotDevice().saveAll(magerLoginInfo, device);

                }
            }





            List<iot_device_task_plain_item> plain_Items = ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == tpguid).ToList();
            foreach (iot_device_task_plain_item item in plain_Items)
            {
                //写入device_task_plain_item
                iot_device_task_plain_item additem = new iot_device_task_plain_item();
                additem.tpi_guid = Guid.NewGuid().ToString();
                additem.tpi_title = item.tpi_title;
                additem.tpi_tp_guid = addplain.tp_guid;

                //查找设备
                iot_device device = ef.iot_device.Where(c => c.d_guid == item.tpi_taskobj_guid && c.d_p_guid == template_plain.tp_p_guid).FirstOrDefault();
                if (device != null)
                {
                    iot_device newDevice = ef.iot_device.Where(c => c.d_p_guid == scene.p_guid && c.d_p_model_no == device.d_p_model_no).FirstOrDefault();
                    if (newDevice != null)
                    {
                        additem.tpi_taskobj_guid = newDevice.d_guid;
                    }
                }
                else
                {
                    additem.tpi_taskobj_guid = item.tpi_taskobj_guid;
                }
                additem.tpi_status = item.tpi_status;
                additem.tpi_type = item.tpi_type;
                additem.tpi_key = item.tpi_key;
                additem.tpi_value = item.tpi_value;
                additem.tpi_value_rlue = item.tpi_value_rlue;
                additem.tpi_order = item.tpi_order;
                additem.tpi_repeat_count = item.tpi_repeat_count;
                additem.tpi_repeat_interval = item.tpi_repeat_interval;
                additem.tpi_mod_time = DateTime.Now;
                additem.tpi_add_time = DateTime.Now;
                additem.tpi_p_model_no = item.tpi_p_model_no;

                EFYihongIotContext addef = new EFYihongIotContext();
                addef.iot_device_task_plain_item.Add(additem);
                addef.SaveChanges();
            }



        }
        public void delProduct(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            EFYihongIotContext efbase = new EFYihongIotContext();
            ef.iot_product.Where(c => c.p_guid == pguid).ExecuteDelete();


            //删除网关
            List<iot_device> iot_Devices = ef.iot_device.Where(c => c.d_p_guid == pguid).ToList();
            foreach (iot_device device in iot_Devices)
            {
                ef.iot_device_gateway.Where(c => c.gw_d_guid == device.d_guid).ExecuteDelete();
            }
            //删除设备
            ef.iot_device.Where(c => c.d_p_guid == pguid).ExecuteDelete();



            List<iot_device_task_plain> plains = ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid).ToList();
            foreach (iot_device_task_plain plain in plains)
            {
                ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == pguid).ExecuteDelete();
                ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid).ExecuteDelete();
            }

            List<iot_device_linkage> linkages = ef.iot_device_linkage.Where(c => c.l_p_guid == pguid).ToList();
            foreach (iot_device_linkage linkage in linkages)
            {
                ef.iot_device_linkage_condition.Where(c => c.lc_l_guid == linkage.l_guid).ExecuteDelete();
                ef.iot_device_linkage.Where(c => c.l_p_guid == pguid).ExecuteDelete();
            }

            List<bs_language_iot_product> languages = efbase.bs_language_iot_product.Where(c => c.lp_p_guid == pguid).ToList();
            foreach (bs_language_iot_product item in languages)
            {
                efbase.bs_language_iot_product_item.Where(c => c.lpi_lp_guid == item.lp_guid).ExecuteDelete();
            }
            efbase.bs_language_iot_product.Where(c => c.lp_p_guid == pguid).ExecuteDelete();


            List<bs_language_product_alone> languagesAlone = efbase.bs_language_product_alone.Where(c => c.lp_p_guid == pguid).ToList();
            foreach (bs_language_product_alone item in languagesAlone)
            {
                efbase.bs_language_product_item_alone.Where(c => c.lpi_lp_guid == item.lp_guid).ExecuteDelete();
            }
            efbase.bs_language_product_alone.Where(c => c.lp_p_guid == pguid).ExecuteDelete();

            


            ef.iot_device_task_time_interval.Where(c => c.ti_p_guid == pguid).ExecuteDelete();
            ef.iot_device_task_time_fixed.Where(c => c.tf_p_guid == pguid).ExecuteDelete();
            ef.iot_device_task_default_run.Where(c => c.dr_p_guid == pguid).ExecuteDelete();
        }

        public void createProduct(string pmguid, string cguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            //try
            //{
            iot_product_model product_Model = ef.iot_product_model.Where(c => c.pm_guid == pmguid).FirstOrDefault();
            iot_product product = new iot_product();//  ef.iot_product.Where(c => c.p_guid == product_Model.pm_p_guid).FirstOrDefault();

            //查编码
            iot_product getcode = ef.iot_product.OrderByDescending(c => c.p_id).FirstOrDefault();
            if (getcode != null)
            {
                string p_code = getcode.p_code.Replace("p", "");
                product.p_code = "p" + (int.Parse(p_code) + 1).ToString();
            }
            else
            {
                product.p_code = "p1001";
            }
            product.p_pm_guid = pmguid;
            product.p_guid = Guid.NewGuid().ToString();
            product.p_name = product_Model.pm_name;
            product.p_check_code = product.p_code + new UtilsStringQuery().GetRandNumber2();
            product.p_add_time = DateTime.Now;
            product.p_c_guid = cguid;
            product.p_remark = "";
            product.p_add_time = DateTime.Now;

            ef.iot_product.Add(product);
            ef.SaveChanges();



            //生成设备
            List<iot_device> list = ef.iot_device.Where(c => c.d_p_guid == product_Model.pm_p_guid).ToList();

            //生成网关
            foreach (iot_device device in list)
            {
                iot_device_model model = ef.iot_device_model.Where(c => c.m_guid == device.d_model_guid).FirstOrDefault();
                if (model.m_attribute == 3)
                {
                    iot_device addGateWaydevice = new UtilsClassClone().Clone(device);
                    //生成网关
                    addGateWaydevice.d_id = 0;
                    //查编码
                    iot_device getdevicecode = ef.iot_device.OrderByDescending(c => c.d_id).FirstOrDefault();
                    if (getcode != null)
                    {
                        addGateWaydevice.d_code = (int.Parse(getdevicecode.d_code) + 1).ToString();
                    }
                    else
                    {
                        addGateWaydevice.d_code = "20001";
                    }
                    addGateWaydevice.d_name = device.d_name;
                    addGateWaydevice.d_guid = Guid.NewGuid().ToString();
                    addGateWaydevice.d_p_guid = product.p_guid;
                    addGateWaydevice.d_is_template = 2;
                    addGateWaydevice.d_add_time = DateTime.Now;
                    addGateWaydevice.d_mod_time = DateTime.Now;
                    addGateWaydevice.d_gw_d_guid = "";
                    addGateWaydevice.d_p_model_no = device.d_p_model_no;
                    //MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                    //magerLoginInfo.userCompanyGuid = cguid;
                    ef.iot_device.Add(addGateWaydevice);
                    ef.SaveChanges();

                  

                    iot_device_gateway rGateway = ef.iot_device_gateway.Where(c => c.gw_d_guid == device.d_guid).FirstOrDefault();
                    //判断是否网关，如果是 写入iot_device_gateway

                    iot_device rdevice = ef.iot_device.Where(c => c.d_guid == addGateWaydevice.d_guid).FirstOrDefault();
                    iot_device_gateway gateway = new iot_device_gateway();
                    gateway.gw_code = rdevice.d_code;
                    gateway.gw_guid = Guid.NewGuid().ToString();
                    gateway.gw_deviceid = "";
                    gateway.gw_system = rGateway.gw_system;
                    gateway.gw_d_guid = addGateWaydevice.d_guid;
                    gateway.gw_add_time = DateTime.Now;
                    ef.iot_device_gateway.Add(gateway);
                    ef.SaveChanges();
                    ef = new EFYihongIotContext();
                    //写入网关下所有设备
                    List<iot_device> gateWayDevicelist = list.Where(c => c.d_gw_d_guid == device.d_guid).ToList();
                    foreach (iot_device item in gateWayDevicelist)
                    {
                        iot_device gateWayDevice = new UtilsClassClone().Clone(item);
                        //生成编码
                        gateWayDevice.d_id = 0;
                        getdevicecode = ef.iot_device.OrderByDescending(c => c.d_id).FirstOrDefault();
                        if (getcode != null)
                        {
                            gateWayDevice.d_code = (int.Parse(getdevicecode.d_code) + 1).ToString();
                        }
                        else
                        {
                            gateWayDevice.d_code = "20001";
                        }
                        gateWayDevice.d_name = gateWayDevice.d_name;
                        gateWayDevice.d_guid = Guid.NewGuid().ToString();
                        gateWayDevice.d_p_guid = product.p_guid;
                        //gateWayDevice.d_p_model_guid = pmguid;
                        gateWayDevice.d_is_template = 2;
                        gateWayDevice.d_add_time = DateTime.Now;
                        gateWayDevice.d_mod_time = DateTime.Now;
                        gateWayDevice.d_gw_d_guid = addGateWaydevice.d_guid;
                        gateWayDevice.d_p_model_no = item.d_p_model_no;
                        ef.iot_device.Add(gateWayDevice);
                        ef.SaveChanges();
                       // magerLoginInfo = new MagerLoginInfo();
                        //magerLoginInfo.userCompanyGuid = cguid;
                       // new ServiceIotDevice().saveAll(magerLoginInfo, gateWayDevice);
                    }
                    //  }
                }
                else if (string.IsNullOrWhiteSpace(device.d_gw_d_guid)) //&&不等于延时
                {
                    //不是网关，又没有绑定网关的设备
                    device.d_id = 0;
                    device.d_name = device.d_name;
                    device.d_guid = Guid.NewGuid().ToString();
                    // device.d_p_model_guid = pmguid;
                    device.d_p_guid = product.p_guid;
                    device.d_is_template = 2;
                    device.d_add_time = DateTime.Now;
                    device.d_mod_time = DateTime.Now;
                    device.d_gw_d_guid = "";
                    device.d_p_model_no = device.d_p_model_no;
                    MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                    magerLoginInfo.userCompanyGuid = cguid;
                    new DBIotDevice().saveAll(magerLoginInfo, device);

                }
            }

            //写入device_task_plain
            List<iot_device_task_plain> template_plains = ef.iot_device_task_plain.Where(c => c.tp_p_guid == product_Model.pm_p_guid).ToList();
            foreach (iot_device_task_plain template_plain in template_plains)
            {



                ef = new EFYihongIotContext();
                iot_device_task_plain addplain = new UtilsClassClone().Clone(template_plain);
                addplain.tp_id = 0;
                addplain.tp_guid = Guid.NewGuid().ToString();
                addplain.tp_title = template_plain.tp_title;
                addplain.tp_status = template_plain.tp_status;
                addplain.tp_mod_time = DateTime.Now;
                addplain.tp_add_time = DateTime.Now;
                addplain.tp_c_guid = cguid;
                addplain.tp_is_template = 2;
                addplain.tp_p_guid = product.p_guid;

                //addplain.tp_p_model_guid = pmguid;
                ef.iot_device_task_plain.Add(addplain);
                ef.SaveChanges();

                List<iot_device_task_plain_item> plain_Items = ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == template_plain.tp_guid).ToList();
                foreach (iot_device_task_plain_item item in plain_Items)
                {
                    //写入device_task_plain_item
                    iot_device_task_plain_item additem = new UtilsClassClone().Clone(item);

                    additem.tpi_id = 0;
                    additem.tpi_guid = Guid.NewGuid().ToString();
                    additem.tpi_title = item.tpi_title;
                    additem.tpi_tp_guid = addplain.tp_guid;

                    //查找设备
                    iot_device device = ef.iot_device.Where(c => c.d_guid == item.tpi_taskobj_guid ).FirstOrDefault();
                    if (device != null)
                    {
                        iot_device newDevice = ef.iot_device.Where(c => c.d_p_guid == product.p_guid && c.d_p_model_no == device.d_p_model_no).FirstOrDefault();
                        if (newDevice != null)
                        {
                            additem.tpi_taskobj_guid = newDevice.d_guid;
                        }
                    }
                    else
                    {
                        additem.tpi_taskobj_guid = item.tpi_taskobj_guid;
                    }
                    additem.tpi_status = item.tpi_status;
                    additem.tpi_type = item.tpi_type;
                    additem.tpi_key = item.tpi_key;
                    additem.tpi_value = item.tpi_value;
                    additem.tpi_value_rlue = item.tpi_value_rlue;
                    additem.tpi_order = item.tpi_order;
                    additem.tpi_repeat_count = item.tpi_repeat_count;
                    additem.tpi_repeat_interval = item.tpi_repeat_interval;
                    additem.tpi_mod_time = DateTime.Now;
                    additem.tpi_add_time = DateTime.Now;
                    additem.tpi_p_model_no = item.tpi_p_model_no;

                    EFYihongIotContext addef = new EFYihongIotContext();
                    addef.iot_device_task_plain_item.Add(additem);
                    addef.SaveChanges();
                }
            }

            //写入触发器
            List<iot_device_linkage> linkages = ef.iot_device_linkage.Where(c => c.l_p_guid == product_Model.pm_p_guid).ToList();
            foreach (iot_device_linkage linkage in linkages)
            {
                iot_device_linkage newLinKage = new UtilsClassClone().Clone(linkage);
                newLinKage.l_id = 0;
                newLinKage.l_guid = Guid.NewGuid().ToString();
                newLinKage.l_p_guid = product.p_guid;
                newLinKage.l_is_template = 2;
                newLinKage.l_add_time = DateTime.Now;
                newLinKage.l_mod_time = DateTime.Now;
                ef.iot_device_linkage.Add(newLinKage);
                ef.SaveChanges();

                List<iot_device_linkage_condition> conditions = ef.iot_device_linkage_condition.Where(c => c.lc_l_guid == linkage.l_guid).ToList();
                foreach (var condition in conditions)
                {
                    iot_device_linkage_condition addcondition = new UtilsClassClone().Clone(condition);
                    addcondition.lc_id = 0;
                    addcondition.lc_guid = Guid.NewGuid().ToString();
                    addcondition.lc_l_guid = newLinKage.l_guid;
                    addcondition.lc_add_time = DateTime.Now;
                    addcondition.lc_mod_time = DateTime.Now;
                    iot_device device = ef.iot_device.Where(c => c.d_guid == condition.lc_d_guid ).FirstOrDefault();
                    if (device != null)
                    {
                        iot_device newDevice = ef.iot_device.Where(c => c.d_p_guid == product.p_guid && c.d_p_model_no == device.d_p_model_no).FirstOrDefault();
                        if (newDevice != null)
                        {
                            addcondition.lc_d_guid = newDevice.d_guid;
                        }
                    }
                    ef.iot_device_linkage_condition.Add(addcondition);
                    ef.SaveChanges();
                }

            }
            EFYihongIotContext eFIotBase = new EFYihongIotContext();

            //写入语言
            List<bs_language_iot_product> languages = eFIotBase.bs_language_iot_product.Where(c => c.lp_p_guid == product_Model.pm_p_guid).ToList();
            foreach (bs_language_iot_product language in languages)
            {
                bs_language_iot_product language_Iot_Product = new UtilsClassClone().Clone(language);
                language_Iot_Product.lp_id = 0;
                language_Iot_Product.lp_p_guid = product.p_guid;
                language_Iot_Product.lp_add_time = DateTime.Now;
                language_Iot_Product.lp_guid = System.Guid.NewGuid().ToString();
                eFIotBase.bs_language_iot_product.Add(language_Iot_Product);
                eFIotBase.SaveChanges();

                List<bs_language_iot_product_item> product_Items = eFIotBase.bs_language_iot_product_item.Where(c => c.lpi_lp_guid == language.lp_guid).ToList();
                foreach (bs_language_iot_product_item productItem in product_Items)
                {
                    bs_language_iot_product_item productItemCopy = new UtilsClassClone().Clone(productItem);
                    productItemCopy.lpi_id = 0;
                    productItemCopy.lpi_lp_guid = language_Iot_Product.lp_guid;
                    productItemCopy.lpi_ci_addtime = DateTime.Now;
                    eFIotBase.bs_language_iot_product_item.Add(productItemCopy);
                    eFIotBase.SaveChanges();
                }
            }
        }


        /// <summary>
        /// 复制产品
        /// </summary>
        /// <param name="pguid"></param>
        /// <param name="pname"></param>
        public void copyProduct(string pguid,string pname)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            //try
            //{
           // iot_product_model product_Model = ef.iot_product_model.Where(c => c.pm_guid == pmguid).FirstOrDefault();
            iot_product product = new iot_product();//  ef.iot_product.Where(c => c.p_guid == product_Model.pm_p_guid).FirstOrDefault();

            //查编码
            iot_product getcode = ef.iot_product.OrderByDescending(c => c.p_id).FirstOrDefault();
            if (getcode != null)
            {
                string p_code = getcode.p_code.Replace("p", "");
                product.p_code = "p" + (int.Parse(p_code) + 1).ToString();
            }
            else
            {
                product.p_code = "p1001";
            }
            //product.p_pm_guid = pmguid;
            product.p_guid = Guid.NewGuid().ToString();
            product.p_name =pname;
            product.p_check_code = product.p_code + new UtilsStringQuery().GetRandNumber2();
            product.p_add_time = DateTime.Now;
            //product.p_c_guid = cguid;
            product.p_remark = "";
            product.p_add_time = DateTime.Now;

            ef.iot_product.Add(product);
            ef.SaveChanges();



            //生成设备
            List<iot_device> list = ef.iot_device.Where(c => c.d_p_guid == pguid).ToList();

            //生成网关
            foreach (iot_device device in list)
            {
                iot_device_model model = ef.iot_device_model.Where(c => c.m_guid == device.d_model_guid).FirstOrDefault();
                if (model.m_attribute == 3)
                {
                    iot_device addGateWaydevice = new UtilsClassClone().Clone(device);
                    //生成网关
                    addGateWaydevice.d_id = 0;
                    //查编码
                    iot_device getdevicecode = ef.iot_device.OrderByDescending(c => c.d_id).FirstOrDefault();
                    if (getcode != null)
                    {
                        addGateWaydevice.d_code = (int.Parse(getdevicecode.d_code) + 1).ToString();
                    }
                    else
                    {
                        addGateWaydevice.d_code = "20001";
                    }
                    addGateWaydevice.d_name = device.d_name;
                    addGateWaydevice.d_guid = Guid.NewGuid().ToString();
                    addGateWaydevice.d_p_guid = product.p_guid;
                    addGateWaydevice.d_is_template = 2;
                    addGateWaydevice.d_add_time = DateTime.Now;
                    addGateWaydevice.d_mod_time = DateTime.Now;
                    addGateWaydevice.d_gw_d_guid = "";
                    addGateWaydevice.d_p_model_no = device.d_p_model_no;
                    //MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                    //magerLoginInfo.userCompanyGuid = cguid;
                    ef.iot_device.Add(addGateWaydevice);
                    ef.SaveChanges();



                    iot_device_gateway rGateway = ef.iot_device_gateway.Where(c => c.gw_d_guid == device.d_guid).FirstOrDefault();
                    //判断是否网关，如果是 写入iot_device_gateway

                    iot_device rdevice = ef.iot_device.Where(c => c.d_guid == addGateWaydevice.d_guid).FirstOrDefault();
                    iot_device_gateway gateway = new iot_device_gateway();
                    gateway.gw_code = rdevice.d_code;
                    gateway.gw_guid = Guid.NewGuid().ToString();
                    gateway.gw_deviceid = "";
                    gateway.gw_system = rGateway.gw_system;
                    gateway.gw_d_guid = addGateWaydevice.d_guid;
                    gateway.gw_add_time = DateTime.Now;
                    ef.iot_device_gateway.Add(gateway);
                    ef.SaveChanges();
                    ef = new EFYihongIotContext();
                    //写入网关下所有设备
                    List<iot_device> gateWayDevicelist = list.Where(c => c.d_gw_d_guid == device.d_guid).ToList();
                    foreach (iot_device item in gateWayDevicelist)
                    {
                        iot_device gateWayDevice = new UtilsClassClone().Clone(item);
                        //生成编码
                        gateWayDevice.d_id = 0;
                        getdevicecode = ef.iot_device.OrderByDescending(c => c.d_id).FirstOrDefault();
                        if (getcode != null)
                        {
                            gateWayDevice.d_code = (int.Parse(getdevicecode.d_code) + 1).ToString();
                        }
                        else
                        {
                            gateWayDevice.d_code = "20001";
                        }
                        gateWayDevice.d_name = gateWayDevice.d_name;
                        gateWayDevice.d_guid = Guid.NewGuid().ToString();
                        gateWayDevice.d_p_guid = product.p_guid;
                        //gateWayDevice.d_p_model_guid = pmguid;
                        gateWayDevice.d_is_template = 2;
                        gateWayDevice.d_add_time = DateTime.Now;
                        gateWayDevice.d_mod_time = DateTime.Now;
                        gateWayDevice.d_gw_d_guid = addGateWaydevice.d_guid;
                        gateWayDevice.d_p_model_no = item.d_p_model_no;
                        ef.iot_device.Add(gateWayDevice);
                        ef.SaveChanges();
                        // magerLoginInfo = new MagerLoginInfo();
                        //magerLoginInfo.userCompanyGuid = cguid;
                        // new ServiceIotDevice().saveAll(magerLoginInfo, gateWayDevice);
                    }
                    //  }
                }
                else if (string.IsNullOrWhiteSpace(device.d_gw_d_guid)) //&&不等于延时
                {
                    //不是网关，又没有绑定网关的设备
                    device.d_id = 0;
                    device.d_name = device.d_name;
                    device.d_guid = Guid.NewGuid().ToString();
                    // device.d_p_model_guid = pmguid;
                    device.d_p_guid = product.p_guid;
                    device.d_is_template = 2;
                    device.d_add_time = DateTime.Now;
                    device.d_mod_time = DateTime.Now;
                    device.d_gw_d_guid = "";
                    device.d_p_model_no = device.d_p_model_no;
                    MagerLoginInfo magerLoginInfo = new MagerLoginInfo();
                   // magerLoginInfo.userCompanyGuid = cguid;
                    new DBIotDevice().saveAll(magerLoginInfo, device);

                }
            }

            //写入device_task_plain
            List<iot_device_task_plain> template_plains = ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid).ToList();
            foreach (iot_device_task_plain template_plain in template_plains)
            {



                ef = new EFYihongIotContext();
                iot_device_task_plain addplain = new UtilsClassClone().Clone(template_plain);
                addplain.tp_id = 0;
                addplain.tp_guid = Guid.NewGuid().ToString();
                addplain.tp_title = template_plain.tp_title;
                addplain.tp_status = template_plain.tp_status;
                addplain.tp_mod_time = DateTime.Now;
                addplain.tp_add_time = DateTime.Now;
                //addplain.tp_c_guid = cguid;
                addplain.tp_is_template = 2;
                addplain.tp_p_guid = product.p_guid;

                //addplain.tp_p_model_guid = pmguid;
                ef.iot_device_task_plain.Add(addplain);
                ef.SaveChanges();

                List<iot_device_task_plain_item> plain_Items = ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == template_plain.tp_guid).ToList();
                foreach (iot_device_task_plain_item item in plain_Items)
                {
                    //写入device_task_plain_item
                    iot_device_task_plain_item additem = new UtilsClassClone().Clone(item);

                    additem.tpi_id = 0;
                    additem.tpi_guid = Guid.NewGuid().ToString();
                    additem.tpi_title = item.tpi_title;
                    additem.tpi_tp_guid = addplain.tp_guid;

                    //查找设备
                    iot_device device = ef.iot_device.Where(c => c.d_guid == item.tpi_taskobj_guid).FirstOrDefault();
                    if (device != null)
                    {
                        iot_device newDevice = ef.iot_device.Where(c => c.d_p_guid == product.p_guid && c.d_p_model_no == device.d_p_model_no).FirstOrDefault();
                        if (newDevice != null)
                        {
                            additem.tpi_taskobj_guid = newDevice.d_guid;
                        }
                    }
                    else
                    {
                        additem.tpi_taskobj_guid = item.tpi_taskobj_guid;
                    }
                    additem.tpi_status = item.tpi_status;
                    additem.tpi_type = item.tpi_type;
                    additem.tpi_key = item.tpi_key;
                    additem.tpi_value = item.tpi_value;
                    additem.tpi_value_rlue = item.tpi_value_rlue;
                    additem.tpi_order = item.tpi_order;
                    additem.tpi_repeat_count = item.tpi_repeat_count;
                    additem.tpi_repeat_interval = item.tpi_repeat_interval;
                    additem.tpi_mod_time = DateTime.Now;
                    additem.tpi_add_time = DateTime.Now;
                    additem.tpi_p_model_no = item.tpi_p_model_no;

                    EFYihongIotContext addef = new EFYihongIotContext();
                    addef.iot_device_task_plain_item.Add(additem);
                    addef.SaveChanges();
                }
            }

            //写入触发器
            List<iot_device_linkage> linkages = ef.iot_device_linkage.Where(c => c.l_p_guid ==pguid).ToList();
            foreach (iot_device_linkage linkage in linkages)
            {
                iot_device_linkage newLinKage = new UtilsClassClone().Clone(linkage);
                newLinKage.l_id = 0;
                newLinKage.l_guid = Guid.NewGuid().ToString();
                newLinKage.l_p_guid = product.p_guid;
                newLinKage.l_is_template = 2;
                newLinKage.l_add_time = DateTime.Now;
                newLinKage.l_mod_time = DateTime.Now;
                ef.iot_device_linkage.Add(newLinKage);
                ef.SaveChanges();

                List<iot_device_linkage_condition> conditions = ef.iot_device_linkage_condition.Where(c => c.lc_l_guid == linkage.l_guid).ToList();
                foreach (var condition in conditions)
                {
                    iot_device_linkage_condition addcondition = new UtilsClassClone().Clone(condition);
                    addcondition.lc_id = 0;
                    addcondition.lc_guid = Guid.NewGuid().ToString();
                    addcondition.lc_l_guid = newLinKage.l_guid;
                    addcondition.lc_add_time = DateTime.Now;
                    addcondition.lc_mod_time = DateTime.Now;
                    iot_device device = ef.iot_device.Where(c => c.d_guid == condition.lc_d_guid).FirstOrDefault();
                    if (device != null)
                    {
                        iot_device newDevice = ef.iot_device.Where(c => c.d_p_guid == product.p_guid && c.d_p_model_no == device.d_p_model_no).FirstOrDefault();
                        if (newDevice != null)
                        {
                            addcondition.lc_d_guid = newDevice.d_guid;
                        }
                    }
                    ef.iot_device_linkage_condition.Add(addcondition);
                    ef.SaveChanges();
                }

            }
            EFYihongIotContext eFIotBase = new EFYihongIotContext();

            //写入语言
            List<bs_language_iot_product> languages = eFIotBase.bs_language_iot_product.Where(c => c.lp_p_guid == pguid).ToList();
            foreach (bs_language_iot_product language in languages)
            {
                bs_language_iot_product language_Iot_Product = new UtilsClassClone().Clone(language);
                language_Iot_Product.lp_id = 0;
                language_Iot_Product.lp_p_guid = product.p_guid;
                language_Iot_Product.lp_add_time = DateTime.Now;
                language_Iot_Product.lp_guid = System.Guid.NewGuid().ToString();
                eFIotBase.bs_language_iot_product.Add(language_Iot_Product);
                eFIotBase.SaveChanges();

                List<bs_language_iot_product_item> product_Items = eFIotBase.bs_language_iot_product_item.Where(c => c.lpi_lp_guid == language.lp_guid).ToList();
                foreach (bs_language_iot_product_item productItem in product_Items)
                {
                    bs_language_iot_product_item productItemCopy = new UtilsClassClone().Clone(productItem);
                    productItemCopy.lpi_id = 0;
                    productItemCopy.lpi_lp_guid = language_Iot_Product.lp_guid;
                    productItemCopy.lpi_ci_addtime = DateTime.Now;
                    eFIotBase.bs_language_iot_product_item.Add(productItemCopy);
                    eFIotBase.SaveChanges();
                }
            }


            //写入语言
            List<bs_language_product_alone> languagesAlone = eFIotBase.bs_language_product_alone.Where(c => c.lp_p_guid == pguid).ToList();
            foreach (bs_language_product_alone language in languagesAlone)
            {
                bs_language_product_alone language_Iot_Product = new UtilsClassClone().Clone(language);
                language_Iot_Product.lp_id = 0;
                language_Iot_Product.lp_p_guid = product.p_guid;
                language_Iot_Product.lp_add_time = DateTime.Now;
                language_Iot_Product.lp_guid = System.Guid.NewGuid().ToString();
                eFIotBase.bs_language_product_alone.Add(language_Iot_Product);
                eFIotBase.SaveChanges();

                List<bs_language_product_item_alone> product_ItemsAlone = eFIotBase.bs_language_product_item_alone.Where(c => c.lpi_lp_guid == language.lp_guid).ToList();
                foreach (bs_language_product_item_alone productItem in product_ItemsAlone)
                {
                    bs_language_product_item_alone productItemCopy = new UtilsClassClone().Clone(productItem);
                    productItemCopy.lpi_id = 0;
                    productItemCopy.lpi_guid = System.Guid.NewGuid().ToString();
                    productItemCopy.lpi_lp_guid = language_Iot_Product.lp_guid;
                    productItemCopy.lpi_addtime = DateTime.Now;
                    eFIotBase.bs_language_product_item_alone.Add(productItemCopy);
                    eFIotBase.SaveChanges();
                }
            }
        }


    }

}
